#!/usr/bin/env bash
echo "请先确保网络连接正常,设置好网络代理等"

# 查找当前目录及其子目录中的所有.sh文件
sh_files=$(find . -name "*.sh" -type f)

# 引入所有.sh文件
for file in $sh_files; do
  if [ "$file" != "./init.sh" ]; then # 排除当前文件
    source "$file"
  fi
done

changeSource
if isInstall "git"; then
  echo "git 已安装"
else
  echo "git 未安装"
fi
# 安装brew
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
if isInstall "brew"; then
  echo "brew 已安装"
  eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
else
  echo "brew 未安装"
  if isMatch "HOMEBREW_API_DOMAIN" "$HOME/.bash_profile"; then
    echo "已设置brew源"
  else
    # 切换brew源 for bash
    echo 'export HOMEBREW_API_DOMAIN="https://mirrors.aliyun.com/homebrew-bottles/api"' >>~/.bash_profile
    echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.aliyun.com/homebrew/brew.git"' >>~/.bash_profile
    echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.aliyun.com/homebrew/homebrew-core.git"' >>~/.bash_profile
    echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.aliyun.com/homebrew/homebrew-bottles"' >>~/.bash_profile
    source ~/.bash_profile
    brew update
  fi
  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
fi

echo "正在配置zsh"
if isInstall "zsh"; then
  echo "zsh 已安装"
  sudo apt install zsh
else
  echo "zsh 未安装"
  sudo apt update
  sudo apt install zsh
fi
# sh
if [ ! -d "$HOME/sh" ]; then
  git clone https://gitee.com/ljstatic/linux-sh.git ~/sh --depth 1
fi
# ohmyzsh
if [ ! -d "$HOME/.oh-my-zsh" ]; then
  echo "安装oh-my-zsh..."
  sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  echo "完成安装oh-my-zsh..."
fi
if [ ! -d "$HOME/.oh-my-zsh/lib" ]; then
  echo "oh-my-zsh安装失败！！！！！！"
  exit 1
fi
if [ ! -d "$HOME/.oh-my-zsh/custom/plugins/zsh-completions" ]; then
  git clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions --depth 1
fi
if [ ! -d "$HOME/.oh-my-zsh/custom/plugins/zsh-autosuggestions" ]; then
  git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions --depth 1
fi
if [ ! -d "$HOME/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting" ]; then
  git clone https://github.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting --depth 1
fi
if [ ! -d "$HOME/.oh-my-zsh/custom/themes/powerlevel10k" ]; then
  git clone --depth 1 https://github.com/romkatv/powerlevel10k ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/powerlevel10k
fi
if [ ! -d "$HOME/.oh-my-zsh/custom/plugins/fzf-tab" ]; then
  git clone --depth 1 https://github.com/Aloxaf/fzf-tab ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/fzf-tab
fi

cp ../.p10k.zsh ~/.p10k.zsh
cp ../.linjrc ~/.linjrc

sh -c 'cat <<EOF 
export LANG='zh_CN.UTF-8'
export LANGUAGE='en_US:en'
export LC_ALL='en_US.UTF-8'
export TERM=xterm

##### Zsh/Oh-my-Zsh Configuration
export ZSH=\$HOME/.oh-my-zsh

ZSH_THEME="powerlevel10k/powerlevel10k"
plugins=(git vscode yarn zsh-completions zsh-autosuggestions z zsh-syntax-highlighting extract  fzf-tab)

source \$ZSH/oh-my-zsh.sh
POWERLEVEL9K_SHORTEN_STRATEGY="truncate_to_last"
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(user dir vcs status)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=()
POWERLEVEL9K_STATUS_OK=false
POWERLEVEL9K_STATUS_CROSS=true
# To customize prompt, run "p10k configure" or edit ~/.p10k.zsh.
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
[[ ! -f ~/.linjrc ]] || source ~/.linjrc
EOF' >$HOME/.zshrc

chsh -s /bin/zsh
# 安装 nvs
echo "正在安装nvs"
cd ~/sh/install && ./init.sh -i nvs

echo "安装完成，请执行 source ~/.zshrc 验证"
